package cn.edu.cqu.fredyvia.forum.controller;

import cn.edu.cqu.fredyvia.forum.entity.CommentThumb;
import cn.edu.cqu.fredyvia.forum.entity.TalkThumb;
import cn.edu.cqu.fredyvia.forum.entity.User;
import cn.edu.cqu.fredyvia.forum.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.Map;

@Controller
@Slf4j
public class ThumbController {
    @Autowired
    ICommentThumbService iCommentThumbService;

    @Autowired
    ITalkThumbService iTalkThumbService;

    @Autowired
    IUserService iUserService;

    @Autowired
    ITalkService iTalkService;

    @Autowired
    ICommentService iCommentService;

    //点赞
    @ResponseBody
//    @Transactional(rollbackFor = {RuntimeException.class})
    @PostMapping("/{type}/thumb/{action}")
    public AjaxResult thumb(@PathVariable String type, @PathVariable String action, @RequestBody Map<String, Object> map) {
//        log.info(_comment_talk_id);
        log.info(map.toString());
        Integer comment_Talk_id = (Integer) map.get("_comment_talk_id");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        User user = iUserService.getUserByUsername(username);
        try {
            if (type.equalsIgnoreCase("talk")) {
                if (action.equalsIgnoreCase("add"))
                    iTalkThumbService.save(new TalkThumb(user.getId(), comment_Talk_id));
                else if (action.equalsIgnoreCase("del"))
                    iTalkThumbService.remove(new QueryWrapper<>(new TalkThumb(user.getId(), comment_Talk_id)));
                else
                    throw new Exception();
//                Talk talk = iTalkService.getById(comment_Talk_id);
//                Integer tmpInteger = talk.getTotal_thumb();
//                talk.setTotal_thumb(tmpInteger + 1);
//                iTalkService.updateById(talk);
            } else if (type.equalsIgnoreCase("comment")) {
                if (action.equalsIgnoreCase("add"))
                    iCommentThumbService.save(new CommentThumb(user.getId(), comment_Talk_id));
                else if (action.equalsIgnoreCase("del"))
                    iCommentThumbService.remove(new QueryWrapper<>(new CommentThumb(user.getId(), comment_Talk_id)));
                else
                    throw new Exception();
//                Comment comment = iCommentService.getById(comment_Talk_id);
//                Integer tmpInteger = comment.getTotal_thumb();
//                comment.setTotal_thumb(tmpInteger + 1);
//                iCommentService.updateById(comment);
            } else
                throw new Exception();
        } catch (SQLException e) {
            return AjaxResult.error("已经点过赞了");
        } catch (Exception e) {
            return AjaxResult.error("路径无效");
        }
        return AjaxResult.success("点赞成功");
    }
}
